package cn.m19.lof;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableDefConfig;
import com.mybatisflex.codegen.template.impl.EnjoyTemplate;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zaxxer.hikari.HikariDataSource;

public class CodeGenerator {

    public static void main(String[] args) {
        // 配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/lof?characterEncoding=utf-8");
        dataSource.setUsername("lof");
        dataSource.setPassword("lof");
        GlobalConfig globalConfig = createGlobalConfig();
        // 通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);
        // 生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfig() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 策略配置
        globalConfig.getStrategyConfig()
            // .setTablePrefix("")
            // .setLogicDeleteColumn("")
            // .setVersionColumn("")
            // .setGenerateForView(false)
            // .setGenerateSchema("")
            // .setIgnoreColumns("")
            // .setUnGenerateTable("")
            .setGenerateTable("sys_account")
        ;

        // 注释配置
        globalConfig.getJavadocConfig()
            // .setSince("")
            // .setEntityPackage("")
            // .setMapperPackage("")
            // .setServicePackage("")
            // .setServiceImplPackage("")
            // .setControllerPackage("")
            .setAuthor("lof")
        ;

        // 模板配置
        globalConfig.getTemplateConfig()
            .setTemplate(new EnjoyTemplate())
            .setEntity(System.getProperty("user.dir") + "/src/main/resources/template/entityOrBase.tpl")
            .setMapper(System.getProperty("user.dir") + "/src/main/resources/template/mapper.tpl")
            .setMapperXml(System.getProperty("user.dir") + "/src/main/resources/template/mapperXml.tpl")
            .setService(System.getProperty("user.dir") + "/src/main/resources/template/service.tpl")
            .setServiceImpl(System.getProperty("user.dir") + "/src/main/resources/template/serviceImpl.tpl")
            .setController(System.getProperty("user.dir") + "/src/main/resources/template/controller.tpl")
            .setTableDef(System.getProperty("user.dir") + "/src/main/resources/template/tableDef.tpl")
        ;

        // 包配置
        globalConfig.getPackageConfig()
            .setSourceDir(System.getProperty("user.dir") + "/src/main/java")
            .setBasePackage("cn.m19.lof")
            .setEntityPackage(globalConfig.getBasePackage() + ".entity")
            .setTableDefPackage(globalConfig.getBasePackage() + ".table")
            .setMapperPackage(globalConfig.getBasePackage() + ".mapper")
            .setServicePackage(globalConfig.getBasePackage() + ".service")
            .setServiceImplPackage(globalConfig.getBasePackage() + ".service.impl")
            .setControllerPackage(globalConfig.getBasePackage() + ".controller")
            .setMapperXmlPath(System.getProperty("user.dir") + "/src/main/resources/mapper")
        ;

        // Entity配置
        globalConfig.enableEntity()
            .setOverwriteEnable(false)
            // .setSuperClass()
            // .setClassPrefix("")
            // .setClassSuffix("")
            // .setOverwriteEnable(false)
            // .setWithSwagger(false)
            // .setWithActiveRecord(false)
            .setWithLombok(true)
            .setLombokNoArgsConstructorEnable(true)
            .setLombokNoArgsConstructorEnable(true)
            .setJdkVersion(21)
        ;

        // Mapper配置
        globalConfig.enableMapper()
            .setOverwriteEnable(false)
            .setMapperAnnotation(true)
            .setSuperClass(BaseMapper.class)
            .setClassPrefix("")
            .setClassSuffix("Mapper")
        ;

        // MapperXml配置
        globalConfig.enableMapperXml()
            .setOverwriteEnable(false)
            .setFilePrefix("")
            .setFileSuffix("Mapper")
        ;

        // Service配置
        globalConfig.enableService()
            .setOverwriteEnable(false)
            .setSuperClass(IService.class)
            .setClassPrefix("")
            .setClassSuffix("Service")
        ;

        // ServiceImpl配置
        globalConfig.enableServiceImpl()
            .setOverwriteEnable(false)
            // .setCacheExample(true)
            .setSuperClass(ServiceImpl.class)
            .setClassPrefix("")
            .setClassSuffix("ServiceImpl")
        ;

        // Controller配置
        globalConfig.enableController()
            // .setRequestMappingPrefix("")
            .setOverwriteEnable(false)
            .setRestStyle(true)
            .setSuperClass(null)
            .setClassPrefix("")
            .setClassSuffix("Controller")
        ;

        // TableDef配置
        globalConfig.enableTableDef()
            .setOverwriteEnable(false)
            .setClassPrefix("")
            .setClassSuffix("Def")
            .setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE)
        ;

        return globalConfig;
    }
}
